package ua.dlab.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import ua.dlab.Algorithm;

public abstract class Step3 implements Step {
	private JComponent pnlMain = null;
	private double[] xkplus1 = null;

	public Step3(double[][] matrix, int matrixSize, int eps) {
		xkplus1 = Algorithm.doWork(matrix, matrixSize, eps);
	}
	
	private void init() {
		pnlMain = new JPanel(new GridBagLayout());
		
		pnlMain.add(new JLabel("Результати:"), 
				new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE, new Insets(6, 6, 6, 6), 0, 0));
		
		if (xkplus1 != null) {
			for (int i = 0; i < xkplus1.length; i++) {
				pnlMain.add(new JLabel("X" + (i + 1) + " = " + String.valueOf(xkplus1[i])), 
						new GridBagConstraints(0, i + 1, 1, 1, 1, 0, GridBagConstraints.NORTHWEST,
								GridBagConstraints.HORIZONTAL, new Insets(6, 6, 6, 6), 0, 0));
			}
		}
		
		pnlMain.add(new JPanel(), 
				new GridBagConstraints(0, (xkplus1 == null || xkplus1.length == 0) ? 0 : xkplus1.length + 1, 1, 1, 1, 1, GridBagConstraints.NORTHWEST,
						GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
	}

	@Override
	public boolean hashNext() {
		return false;
	}

	@Override
	public boolean hashPrevious() {
		return true;
	}

	@Override
	public Step nextStep() {
		return null;
	}
	
	@Override
	public Action getAction() {
		return new AbstractAction("Зберегти результати") {
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent e) {
				save();
			}
		};
	}
	
	private void save() {
		JFileChooser jfcSave = new JFileChooser();
		jfcSave.setFileFilter(new FileNameExtensionFilter("Txt files (*.txt)", "txt"));
		
		if (jfcSave.showSaveDialog(pnlMain) == JFileChooser.APPROVE_OPTION) {
			if (jfcSave.getSelectedFile().exists()) {
				int answer = JOptionPane.showConfirmDialog(pnlMain, 
						"Файл вже існує. Ви точно бажаєте його перезаписати?", "Перезапис файлу", JOptionPane.YES_NO_OPTION);
				if (answer == JOptionPane.YES_OPTION) {
					saveFile(jfcSave.getSelectedFile().getAbsolutePath());
				}
			} else {
				saveFile(jfcSave.getSelectedFile().getAbsolutePath());
			}
		}
	}

	private void saveFile(String absolutePath) {
		absolutePath = absolutePath.endsWith(".txt") ? absolutePath : absolutePath + ".txt";
		
		if (xkplus1 != null) {
			try {
				Path path = Paths.get(absolutePath);
				try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
					int counter = 1;
					
					for (double line : xkplus1) {
						writer.write("X" + counter++ + " = " + String.valueOf(line) + ";");
						writer.newLine();
					}
				}
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public abstract Step previousStep();

	@Override
	public JComponent getMainArea() {
		if (pnlMain == null) {
			init();
		}
		
		return new JScrollPane(pnlMain);
	}
}
